package com.system.controller;

import com.system.controller.entity.Letter;
import com.system.controller.entity.LetterResult;
import com.system.controller.entity.Request;
import com.system.controller.entity.Result;
import com.system.po.Course;
import com.system.po.CourseCustom;
import com.system.po.PagingVO;
import com.system.po.Userlogin;
import com.system.service.CourseService;
import com.system.service.UserloginService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/openApi")
public class OpenApiController {

    @Resource(name = "userloginServiceImpl")
    private UserloginService userloginService;

    @Resource(name = "courseServiceImpl")
    private CourseService courseService;

    //  学生信息显示
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody Request request) {
        Result result = new Result();

        try {
            if (request.getEmail() != null) {
                //验证邮箱和密码是否正确
                Userlogin byEmail = userloginService.findByEmail(request.getEmail());
                if (byEmail != null) {
                    if (request.getPassPwd().equals(byEmail.getPassword())) {
                        result.setStatus("200");
                        result.setMsg("登录成功!!");
                    } else {
                        result.setStatus("400");
                        result.setMsg("登录失败，用户密码错误");
                    }
                } else {
                    result.setStatus("400");
                    result.setMsg("登录失败，无该邮箱名");
                }
            } else if (request.getUserName() != null) {
                //验证用户名和密码是否正确
                Userlogin byName = userloginService.findByName(request.getUserName());
                if (byName != null) {
                    if (request.getPassPwd().equals(byName.getPassword())) {
                        result.setStatus("200");
                        result.setMsg("登录成功!!");
                    } else {
                        result.setStatus("400");
                        result.setMsg("登录失败，用户密码错误");
                    }
                } else {
                    result.setStatus("400");
                    result.setMsg("登录失败，无该用户名");
                }


            } else {
                result.setStatus("400");
                result.setMsg("登录失败，请检查是否输入了用户名或者邮箱");
            }
        } catch (Exception e) {
            result.setStatus("400");
            result.setMsg("登录失败，接口异常，请联系管理员");
        }

        return result;
    }

    //  学生信息显示
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(@RequestBody Request request) {
        Result result = new Result();

        try {
            //判断注册用户是否存在
            //验证用户名和密码是否正确
            Userlogin byName = userloginService.findByName(request.getUserName());
            if (byName != null) {
                result.setStatus("400");
                result.setMsg("注册失败,该用户已存在数据库中");
            } else {
                Userlogin userlogin = new Userlogin();
                userlogin.setUsername(request.getUserName());
                userlogin.setEmail(request.getEmail());
                userlogin.setPassword(request.getPassPwd());
                userlogin.setRole(0);
                userloginService.save(userlogin);
                result.setStatus("200");
                result.setMsg("注册成功");
            }

        } catch (Exception e) {
            result.setStatus("400");
            result.setMsg("注册失败，接口异常，请联系管理员");
        }

        return result;
    }

    //保存信件内容
    @RequestMapping(value = "/saveLetter", method = RequestMethod.POST)
    public Result saveLetter(@RequestBody CourseCustom course) {
        Result result = new Result();

        try {
            course.setCollegeid(1);
            course.setCoursetype("0");
            courseService.save(course);
            result.setStatus("200");
            result.setMsg("保存成功");

        } catch (Exception e) {
            result.setStatus("400");
            result.setMsg("保存信件内容失败，接口异常，请联系管理员");
        }

        return result;
    }

    @RequestMapping(value = "/showLetter", method = RequestMethod.POST)
    public LetterResult showLetter(@RequestBody Request request) throws Exception {

        Integer page = request.getPage();
        LetterResult result = new LetterResult();
        List<CourseCustom> list = null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        //设置总页数
        pagingVO.setTotalCount(courseService.getCountCouse());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = courseService.findByPaging(1);
        } else {
            pagingVO.setToPageNo(page);
            list = courseService.findByPaging(page);
        }

        if (CollectionUtils.isNotEmpty(list)) {
            result.setStatus("200");
            result.setMsg("获取成功");
            List<Letter> letters = new ArrayList<Letter>();
            for (CourseCustom courseCustom:list) {
                Letter letter = new Letter();
                letter.setcollegeName(courseCustom.getcollegeName());
                letter.setCourseid(courseCustom.getCourseid());
                letter.setCoursename(courseCustom.getCoursename());
                letter.setTeacherid(courseCustom.getTeacherid());
                letter.setCoursetime(courseCustom.getCoursetime());
                letter.setClassroom(courseCustom.getClassroom());
                letter.setCourseweek(courseCustom.getCourseweek());
                letter.setCoursetype(courseCustom.getCoursetype());
                letter.setCollegeid(courseCustom.getCollegeid());
                letter.setScore(courseCustom.getScore());
                letters.add(letter);
            }

            result.setData(letters);
        } else {
            result.setStatus("400");
            result.setMsg("获取失败,无多余数据");
        }

        return result;
    }
}
